package com.flag.liutao.modules.equip.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.convert.Convert;
import cn.hutool.core.lang.TypeReference;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.flag.liutao.common.handler.MyException;
import com.flag.liutao.modules.equip.domain.Clothing;
import com.flag.liutao.modules.equip.domain.Equipment;
import com.flag.liutao.modules.equip.mapper.EquipmentMapper;
import com.flag.liutao.modules.equip.param.ClothingParam;
import com.flag.liutao.modules.equip.param.EquipmentParam;
import com.flag.liutao.modules.equip.service.IEquipmentService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.flag.liutao.modules.equip.vo.ClothingVo;
import com.flag.liutao.modules.equip.vo.EquipmentVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author godM
 * @since 2022-12-19
 */
@Service
public class EquipmentServiceImpl extends ServiceImpl<EquipmentMapper, Equipment> implements IEquipmentService {

    @Autowired
    private EquipmentMapper equipmentMapper;
    @Override
    public Map<String, Object> getPage(int page, int limit, String keyword) {
        LambdaQueryWrapper<Equipment> lqw = new LambdaQueryWrapper<>();
        lqw.like(Equipment::getName,keyword).or().like(Equipment::getUnit,keyword).or();
        IPage<Equipment> iPage = new Page<>(page,limit);
        IPage<Equipment> pageDB = equipmentMapper.selectPage(iPage, lqw);
        List<EquipmentVo> convert = Convert.convert(new TypeReference<List<EquipmentVo>>() {
        }, pageDB.getRecords());
        Map<String,Object> map = new HashMap<>();
        map.put("content",convert);
        map.put("total",pageDB.getTotal());
        return map;
    }

    @Override
    public void add(EquipmentParam param) {
        Equipment equipment = new Equipment();
        BeanUtil.copyProperties(param,equipment);
        equipmentMapper.insert(equipment);
    }

    @Override
    public void edit(EquipmentParam param) {
        Equipment equipment = equipmentMapper.selectById(param.getId());
        if(ObjectUtil.isNull(equipment)){
            throw new MyException("该服装不存在");
        }
        BeanUtil.copyProperties(param,equipment);
        equipmentMapper.updateById(equipment);
    }

    @Override
    public void del(Long id) {
        equipmentMapper.deleteById(id);
    }
}
